//
//  RealHouseDetailViewController.m
//  Utils
//
//  Created by enghou on 2018/6/29.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "RealHouseDetailViewController.h"
#import <TZImagePickerController.h>
#import "NetWorkerManager.h"
@interface RealHouseDetailViewController ()

//@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
//
//@property (weak, nonatomic) IBOutlet SelectedButton *isShopHouseBtn;
//
//@property (weak, nonatomic) IBOutlet SelectedButton *isOtherHouseBtn;
//
//@property (weak, nonatomic) IBOutlet UITextField *houseLocateDetailField;
//
//@property (weak, nonatomic) IBOutlet UITextField *houseUsageField;
//@property(nonatomic,strong)NSArray *houseUsages;
//@property(nonatomic,strong)PropertyDictItemModel *houseUsageModel;
//
//@property (weak, nonatomic) IBOutlet UITextField *buildingAreaField;
//
//@property (weak, nonatomic) IBOutlet UITextField *manipulatorNameField;
//
//@property (weak, nonatomic) IBOutlet UITextField *cardTypeField;
//@property(nonatomic,strong)NSArray *cardTypes;
//@property(nonatomic,strong)PropertyDictItemModel *cardModel;
//
//@property (weak, nonatomic) IBOutlet UITextField *cardNumberField;
//
//@property (weak, nonatomic) IBOutlet UITextField *manipulatorMobileField;
//
//@property (weak, nonatomic) IBOutlet UITextField *manipulatorTelField;
//
//@property (weak, nonatomic) IBOutlet UITextField *manipulatorNowLocateField;
//
//@property (weak, nonatomic) IBOutlet SelectedButton *isRentHouseBtn;
//
//@property (weak, nonatomic) IBOutlet SelectedButton *notRentHouseBtn;
//
//@property (weak, nonatomic) IBOutlet UITextField *xiaoquField;
//@property (weak, nonatomic) IBOutlet UITextField *zhuangField;
//@property (weak, nonatomic) IBOutlet UITextField *danyuanField;
//@property (weak, nonatomic) IBOutlet UITextField *houseNumField;
//
//@property (weak, nonatomic) IBOutlet UITableViewCell *otherHouseDetailCell;
//
//@property (weak, nonatomic) IBOutlet UITableViewCell *shopHouseDetailCell;
//
//@property(nonatomic,copy)NSString *filePath;

@end

@implementation RealHouseDetailViewController

-(instancetype)init{
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"House" bundle:[NSBundle mainBundle]];
    return [story instantiateViewControllerWithIdentifier:@"RealHouseDetailViewController"];
}

- (IBAction)isRentHouseAction:(UIButton *)sender {
    sender.selected = YES;
    self.notRentHouseBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)notRentHouseAction:(UIButton *)sender {
    sender.selected = YES;
    self.isRentHouseBtn.selected = NO;
    [self.tableView reloadData];
}

- (IBAction)isShopHouseAction:(UIButton *)sender {
    sender.selected = YES;
    self.isOtherHouseBtn.selected = NO;
//    self.selectedHouseTypeId
    for (PropertyDictItemModel *model in self.houseTypes) {
        if ([model.displayName isEqualToString:@"商品房"]) {
            self.selectedHouseTypeId = model.ids;
        }
    }
    [self.tableView reloadData];
}

- (IBAction)isOtherHouseAction:(UIButton *)sender {
    sender.selected = YES;
    self.isShopHouseBtn.selected = NO;
    for (PropertyDictItemModel *model in self.houseTypes) {
        if ([model.displayName isEqualToString:@"其他"]) {
            self.selectedHouseTypeId = model.ids;
        }
    }
    [self.tableView reloadData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [[NetWorkerManager sharedInstance]getPropertyDictWithDomainName:@"现居住址类型" successBlock:^(BaseModel *model) {
        [self endLoading];
        if (model.success) {
            self.houseTypes = model.list;
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

#pragma mark - 获取详情
-(NSString *)detailPath{
    return @"/mobile/comprehensiveGovernance/houseInfo/houseInfoDetail.action";
}

-(NSDictionary *)detailParam{
    return @{@"id":self.ids};
}

-(void(^)(NSDictionary *))configView{
    void(^config)(NSDictionary *) = ^(NSDictionary *source){
        NSDictionary *data = source[@"data"];
        self.houseLocateDetailField.text = data[@"address"];
        
        NSDictionary *addressType = data[@"addressType"];
        if ([addressType[@"displayName"] isEqualToString:@"商品房"]) {
            self.isShopHouseBtn.selected = YES;
            self.isOtherHouseBtn.selected = NO;
        }else{
            self.isShopHouseBtn.selected = NO;
            self.isOtherHouseBtn.selected = YES;
        }
        self.zhuangField.text = data[@"block"];
        self.danyuanField.text = data[@"unit"];
        self.houseNumField.text = data[@"room"];
        self.xiaoquField.text = data[@"community"];
        self.houseUsageModel = [PropertyDictItemModel new];
        self.houseUsageModel.ids = data[@"houseUses"][@"id"];
        self.houseUsageField.text = data[@"houseUses"][@"displayName"];
        self.buildingAreaField.text = [NSString stringWithFormat:@"%.2f",[data[@"houseArea"] doubleValue]];
        self.cardModel = [PropertyDictItemModel new];
        self.cardModel.ids = data[@"certificateType"][@"id"];
        self.cardTypeField.text = data[@"certificateType"][@"displayName"];
        self.cardNumberField.text = data[@"certificateNumbe"];
        self.manipulatorNameField.text = data[@"name"];
        self.manipulatorMobileField.text = data[@"propertyPersonMobile"];
        self.manipulatorTelField.text = data[@"propertyPersonTel"];
        self.manipulatorNowLocateField.text = data[@"nowLiveAddress"];
        NSString *rental = data[@"rentalHouse"];
        if ([rental isEqualToString:@"是"]) {
            self.isRentHouseBtn.selected = YES;
            self.notRentHouseBtn.selected = NO;
        }else{
            self.isRentHouseBtn.selected = NO;
            self.notRentHouseBtn.selected = YES;
        }
    };
    return config;
}

#pragma 更新记录
-(NSString *)updatePath{
    return @"/mobile/comprehensiveGovernance/houseInfo/updateHouseInfo.action";
}

-(NSDictionary *)updateParameter{
    return [self addedParameter];
}

-(void)updateRecord{
    NSDictionary *param = [self updateParameter];
    if (!param) {
        return;
    }
    if (self.headImageView.image) {
        NSData *data = UIImageJPEGRepresentation(self.headImageView.image, 0.1);
        [self uploadFiles:@[data] successBlock:^(NSArray<FileItemModel *> *files) {
            self.filePath = files[0].path;
            [super updateRecord];
        }];
    }else{
        [super updateRecord];
    }
}

//新增记录
-(NSString *)addPath{
    return @"/mobile/comprehensiveGovernance/houseInfo/addHouseInfo.action";
}
//现居住址类型
-(NSDictionary *)addedParameter{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (self.ids.length > 0) {
        param[@"houseInfo.id"] = self.ids;
    }
    NSString *orgId = [[LocalPersistent sharedInstance]contentForKey:@"orgId"];
    
    if (orgId) {
        param[@"houseInfo.organization.id"] = orgId;
    }
    
    if (self.isShopHouseBtn.selected) {
        param[@"houseInfo.addressType.id"] = self.selectedHouseTypeId;
        if (self.xiaoquField.text.length > 0) {
            param[@"houseInfo.community"] = self.xiaoquField.text;
            if (self.zhuangField.text.length > 0) {
                param[@"houseInfo.block"] = self.zhuangField.text;
                if (self.danyuanField.text.length > 0) {
                    param[@"houseInfo.unit"] = self.danyuanField.text;
                    if (self.houseNumField.text.length > 0) {
                        param[@"houseInfo.room"] = self.houseNumField.text;
                    }else{
                        [self alertSome:@"请输入室号"];
                        return nil;
                    }
                }else{
                    [self alertSome:@"请输入单元"];
                    return nil;
                }
            }else{
                [self alertSome:@"请输入幢号"];
                return nil;
            }
        }else{
            [self alertSome:@"请输入小区"];
            return nil;
        }
    }else{
        param[@"houseInfo.addressType.id"] = self.selectedHouseTypeId;
        if (self.houseLocateDetailField.text.length > 0) {
            param[@"houseInfo.address"] = self.houseLocateDetailField.text;
        }else{
            [self alertSome:@"请输入房屋详址"];
            return nil;
        }
    }
    
    if (self.houseUsageModel.ids.length > 0) {
        param[@"houseInfo.houseUses.id"] = self.houseUsageModel.ids;
    }
    
    if (self.buildingAreaField.text.length > 0) {
        param[@"houseInfo.houseArea"] = self.buildingAreaField.text;
    }
    
    if (self.manipulatorNameField.text.length > 0) {
        param[@"houseInfo.name"] = self.manipulatorNameField.text;
    }else{
        [self alertSome:@"请输入房主姓名!"];
        return nil;
    }
    
    if (self.cardModel.ids.length > 0) {
        param[@"houseInfo.certificateType.id"] = self.cardModel.ids;
    }else{
        [self alertSome:@"请选择证件类型!"];
        return nil;
    }
    
    if (self.cardNumberField.text.length > 0) {
        param[@"houseInfo.certificateNumbe"] = self.cardNumberField.text;
    }else{
        [self alertSome:@"请输入证件号码!"];
        return nil;
    }
    
    
    if (self.manipulatorMobileField.text.length > 0) {
        param[@"houseInfo.propertyPersonMobile"] = self.manipulatorMobileField.text;
    }else{
        [self alertSome:@"请输入房主手机号"];
        return nil;
    }
    
    if (self.manipulatorTelField.text.length > 0) {
        param[@"houseInfo.propertyPersonTel"] = self.manipulatorTelField.text;
    }else{
        [self alertSome:@"请输入房主座机"];
        return nil;
    }
    
    if (self.manipulatorNowLocateField.text.length > 0) {
        param[@"houseInfo.nowLiveAddress"] = self.manipulatorNowLocateField.text;
    }else{
        [self alertSome:@"请输入房主现住地址"];
        return nil;
    }
    
    if (self.isRentHouseBtn.selected) {
        param[@"houseInfo.isRentalHouse"] = @"是";
    }else{
        param[@"houseInfo.isRentalHouse"] = @"否";
    }
    
    if (self.filePath.length > 0) {
        param[@"houseInfo.fileStorage.path"] = self.filePath;
    }
    
    return param;
}

-(void)addRecord{
    NSDictionary *param = [self addedParameter];
    if (!param) {
        return;
    }
    if (self.headImageView.image) {
        NSData *data = UIImageJPEGRepresentation(self.headImageView.image, 0.1);
        [self uploadFiles:@[data] successBlock:^(NSArray<FileItemModel *> *files) {
            self.filePath = files[0].path;
            [super addRecord];
        }];
    }else{
        [super addRecord];
    }
}

-(void)uploadFiles:(NSArray <NSData *>*)files successBlock:(void(^)(NSArray <FileItemModel *>*))suc{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]uploadImageWithPath:kUploadFilePath parameter:nil files:files fileName:@"file" suffix:@"jpg" completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source listItemClass:[FileItemModel class]];
        if (model.success) {
            if (suc) {
                suc(model.list);
            }
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}
//删除记录
-(NSString *)delPath{
    return @"/mobile/comprehensiveGovernance/houseInfo/deleteHouseInfo.action";
}

-(NSDictionary *)delParameter{
    return @{@"id":self.ids};
}

//操作完成后发送的通知
-(NSString *)refresh_not_name{
    return @"refresh_real_house";
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 2:
        {
            self.otherHouseDetailCell.hidden = !self.isOtherHouseBtn.selected;
            return self.otherHouseDetailCell;
        }
            break;
        case 3:
        {
            self.shopHouseDetailCell.hidden = !self.isShopHouseBtn.selected;
            return self.shopHouseDetailCell;
        }
        default:
            return [super tableView:tableView cellForRowAtIndexPath:indexPath];
            break;
    }
    return nil;
}

-(void)showPicker{
    TZImagePickerController *picker = [[TZImagePickerController alloc]initWithMaxImagesCount:1 delegate:nil];
    picker.didFinishPickingPhotosHandle = ^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        self.headImageView.image = photos[0];
    };
    UIViewController *rootVC = [UIApplication sharedApplication].delegate.window.rootViewController;
    [rootVC presentViewController:picker animated:YES completion:nil];
}

-(void)showUsage{
    [self commonAlertWithContent:@"房屋用途" choosenBlock:^(PropertyDictItemModel *model) {
        self.houseUsageModel = model;
        self.houseUsageField.text = model.displayName;
    } getList:^(NSArray *list) {
        self.houseUsages = list;
    }];
}

-(void)showCardType{
    [self commonAlertWithContent:@"证件代码" choosenBlock:^(PropertyDictItemModel *model) {
        self.cardModel = model;
        self.cardTypeField.text = model.displayName;
    } getList:^(NSArray *list) {
        self.cardTypes = list;
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            [self showPicker];
            break;
        case 4:
            [self showUsage];
            break;
        case 7:
            [self showCardType];
            break;
        default:
            break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 2:
            return self.isOtherHouseBtn.selected ? 44 : 0.0;
            break;
        case 3:
            return self.isShopHouseBtn.selected ? 92 : 0.0;
            break;
        default:
            return [super tableView:tableView heightForRowAtIndexPath:indexPath];
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
